package com.shz;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.thread.ThreadUtil;
import com.shz.utils.WinCmd;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.util.concurrent.TimeUnit;

/**
 * @author myy
 * @date 2024/3/5 -- 18:18
 */
@SpringBootApplication
@Slf4j
@RestController
public class Application {


    static String SERVER_PORT = "server.port";
    static String SPRING_APPLICATION_NAME = "spring.application.name";
    static String CONTEXT_PATH = "server.servlet.context-path";

    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext run = SpringApplication.run(Application.class, args);
        Environment env = run.getEnvironment();
        String portAndPath = env.getProperty(SERVER_PORT) + env.getProperty(CONTEXT_PATH);
        log.info(
                "\n----------------------------------------------------------\n\t"
                        + "Application '{}' is running! Access URLs:\n\t" + "Local: \t\thttp://localhost:{}\n\t"
                        + "Doc: \t\thttp://localhost:{}/doc.html\n\t"
                        + "External: \thttp://{}:{}\n\t"
                        + "\n----------------------------------------------------------",
                env.getProperty(SPRING_APPLICATION_NAME), portAndPath, portAndPath,
                InetAddress.getLocalHost().getHostAddress(), portAndPath);
    }

    @GetMapping("/upgrade")
    public void upgrade() {
        new Thread(() -> {
            try {
                log.info("复制文件");
                // 复制前端文件夹
                File srcFile = new File("D:/parking-local/upgrade/dist");
                File destFile = new File("D:/parking-local/parking-local-front/dist");
                FileUtil.copyContent(srcFile, destFile, true);
                // 处理java程序
                log.info("关闭项目:{}", WinCmd.getInstance().execute("taskkill /f /t /im parking-local-java.exe").getOutput());
                ThreadUtil.sleep(2, TimeUnit.SECONDS);
                File srcFile1 = new File("D:/parking-local/upgrade/parking-local-java.exe");
                File destFile1 = new File("D:/parking-local/parking-local-java/parking-local-java.exe");
                FileUtil.copy(srcFile1, destFile1, true);
                log.info("运行项目parking-local-java:{}", WinCmd.getInstance().execute("rundll32 url.dll FileProtocolHandler file://D:/parking-local/parking-local-java/parking-local-java.exe").getOutput());
                log.info("运行项目nginx:{}", WinCmd.getInstance().execute("rundll32 url.dll FileProtocolHandler file://D:/parking-local/nginx-1.23.0/nginx.exe").getOutput());
                log.info("运行项目webrtc-streamer:{}", WinCmd.getInstance().execute("rundll32 url.dll FileProtocolHandler file://D:/parking-local/webrtc/webrtc-streamer.exe").getOutput());
                log.info("打开浏览器:{}", WinCmd.getInstance().execute("rundll32 url.dll FileProtocolHandler http://localhost:3003").getOutput());
                log.info("完成");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();
    }


    /**
     * 开启windows系统的nginx
     */
    private static void startWinProc() throws IOException {
        log.info("运行nginx");
        String myExe = "cmd /c start nginx";
        String preUrl = "D:/parking-local/nginx-1.23.0/";
        File dir = new File(preUrl);
        String[] str = new String[]{};
        // 执行命令
        Runtime.getRuntime().exec(myExe, str, dir);
    }
}
